﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Security;

namespace Blog
{
    public partial class Site : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //Lave et dropdown element for hver theme der er tilgængeligt
                if (Directory.Exists(Server.MapPath("~/App_Themes")))
                {
                    string[] subdirs = Directory.GetDirectories(Server.MapPath("~/App_Themes"));
                    foreach (string dir in subdirs)
                    {
                        DirectoryInfo dirInfo = new DirectoryInfo(dir);
                        uiThemeList.Items.Add(dirInfo.Name);
                    }
                }
                //Hvis brugeren har en cookie har den tidligere valgt et tema : aktivér det
                HttpCookie cookie = Request.Cookies["theme cookie"];
                if (cookie != null)
                {
                    foreach(ListItem li in uiThemeList.Items)
                    {
                        if(cookie.Value == li.Value)
                        {
                            li.Selected = true;
                        }
                    }
                }
                
                RolePrincipal r = (RolePrincipal)HttpContext.Current.User;
                string[] userRoles = r.GetRoles();
                string usr = Context.User.Identity.Name;
                string f = HttpContext.Current.User.Identity.IsAuthenticated.ToString();
                Controls.Add(new LiteralControl("<h1>"+f+"</h1>"));
            }

            if (Page.IsPostBack)
            {
                //HandleLogin();
            }
        }

        //private void HandleLogin()
        protected void HandleLogin(object sender, EventArgs e)
        {
            if (FormsAuthentication.Authenticate(uiUsername.Text, uiPassword.Text))
            {
                Controls.Add(new LiteralControl("<h1>Access!!</h1>"));
                FormsAuthentication.SetAuthCookie(uiUsername.Text, false);
                FormsAuthentication.RedirectFromLoginPage(uiUsername.Text, false);
            }
            else
            {
                //Controls.Clear();
                Controls.Add(new LiteralControl("<h1>Access Denied</h1>"));
            }
        }

        protected void HandleThemeSwitch(object sender, EventArgs e)
        {
            if (uiThemeList.SelectedIndex != 0) { 
                HttpCookie cookie = new HttpCookie("theme cookie");

                //Sæt en cookie
                cookie.Value = uiThemeList.SelectedValue;

                //cookie udløb (10 dage her)
                DateTime dtNow = DateTime.Now;
                TimeSpan tsDays = new TimeSpan(10, 0, 0, 0);
                cookie.Expires = dtNow + tsDays;
                //tilføj cookie
                Response.Cookies.Add(cookie);
                // 
                Response.Redirect(Request.FilePath);
            }
        }
    }
}